package com.aidex.web.controller.biz;

import java.util.List;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.domain.R;
import com.github.pagehelper.PageInfo;
import com.aidex.common.core.page.PageDomain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.poi.ExcelUtil;
import javax.validation.constraints.*;
import org.springframework.web.bind.annotation.*;
import com.aidex.common.core.controller.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.aidex.biz.domain.HousingInfo;
import com.aidex.biz.service.HousingInfoService;
import com.aidex.common.utils.SecurityUtils;

/**
 * 小区信息Controller
 * @author sean
 * @email 2074844820@qq.com
 * @date 2022-09-23
 */
@RestController
@RequestMapping("/biz/bizHousingInfo")
public class HousingInfoController extends BaseController
{
    @Autowired
    private HousingInfoService housingInfoService;

    /**
     * 查询小区信息列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizHousingInfo:list')")
    @GetMapping("/list")
    public R<PageInfo> list(HousingInfo housingInfo, HttpServletRequest request, HttpServletResponse response)
    {
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            housingInfo.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
         }
        housingInfo.setPage(new PageDomain(request, response));
        return R.data(housingInfoService.findPage(housingInfo));
    }

    /**
     * 获取小区信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizHousingInfo:query')")
    @GetMapping(value = "/{id}")
    public R<HousingInfo> detail(@PathVariable("id") String id)
    {
        return R.data(housingInfoService.get(id));
    }

    /**
     * 新增小区信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizHousingInfo:add')")
    @Log(title = "小区信息", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated  HousingInfo housingInfo)
    {
        return R.status(housingInfoService.save(housingInfo));
    }

    /**
     * 修改小区信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizHousingInfo:edit')")
    @Log(title = "小区信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated HousingInfo housingInfo)
    {
        return R.status(housingInfoService.save(housingInfo));
    }


    /**
     * 删除小区信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizHousingInfo:remove')")
    @Log(title = "小区信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids)
    {
        return R.status(housingInfoService.deleteHousingInfoByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @PreAuthorize("@ss.hasPermi('biz:bizHousingInfo:query')")
    @GetMapping("/findMaxSort")
    public R findMaxSort()
    {
        return R.data(housingInfoService.findMaxSort(new HousingInfo()));
    }

    /**
     * 导出小区信息列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizHousingInfo:export')")
    @Log(title = "小区信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(HousingInfo housingInfo)
    {

            if(SecurityUtils.isAdmin(SecurityUtils.getUserId())){
                    housingInfo.setCompanyId(null);
            }else{
                         housingInfo.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
                     }
        List<HousingInfo> list = housingInfoService.findList(housingInfo);
        ExcelUtil<HousingInfo> util = new ExcelUtil<HousingInfo>(HousingInfo.class);
        return util.exportExcel(list, "小区信息数据");
    }

}
